package com.cdivtc.management.service;

import com.cdivtc.management.dao.StudentSelectionMapper;
import com.cdivtc.management.dao.TeacherMapper;
import com.cdivtc.management.dao.TopicTitleMapper;
import com.cdivtc.management.pojo.*;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherTopicService {

    @Autowired
    private TopicTitleMapper topicTitleMapper;

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentSelectionService studentSelectionService;
    @Autowired
    private StudentSelectionMapper studentSelectionMapper;

    public TopicTitle addTopic(TopicTitle topicTitle) {
        if (topicTitle != null) {
            topicTitle.setId(idWorker.nextId() + "");
            topicTitle.setStatus("0");
            topicTitle.setCount("0");
            int insert = topicTitleMapper.insert(topicTitle);
            if (insert > 0) {
                return topicTitle;
            }
        }
        return null;
    }

    public TopicTitle updateTopic(TopicTitle topicTitle) {
        if (topicTitle.getId() != null) {
            Example example = new Example(TopicTitle.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id", topicTitle.getId());
            TopicTitle topicTitle1 = topicTitleMapper.selectByPrimaryKey(topicTitle.getId());
            topicTitle.setCount(topicTitle1.getCount());
            int insert = topicTitleMapper.updateByExample(topicTitle, example);
            if (insert > 0) {
                return topicTitle;
            }
        }
        return null;
    }

    public TopicTitle deleteTopic(TopicTitle topicTitle) {
        if (topicTitle != null) {
            int insert = topicTitleMapper.delete(topicTitle);
            if (insert > 0) {
                return topicTitle;
            }
        }
        return null;
    }

    public List<TopicTitle> selTopic(String str, String status, String teaId) {
        System.out.println("str = " + str);
        Example example = new Example(TopicTitle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("teaId", teaId);
        criteria.andLike("title", "%" + str + "%");
        if (status != null) {
            criteria.andEqualTo("status", status);
        }
        List<TopicTitle> topicTitles = topicTitleMapper.selectByExample(example);
        return topicTitles;
    }

    //学生根据查询老师课题
    public List<Map<String, Object>> selTeaTopic(Student stu) {
        System.out.println("TeacherTopicService line 74= " + stu);

        String college = stu.getCollege();
        //根据学生所在学院查询 学院老师Id
        Example example1 = new Example(Teacher.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("college", stu.getCollege());
        List<Teacher> teachers = teacherMapper.selectByExample(example1);
        ArrayList<String> strings = new ArrayList<>();
        for (Teacher teacher : teachers) {
            System.out.println("TeacherTopicService line 96= " + teacher.getId());
            strings.add(teacher.getId());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        if(strings.size()>0){
            //根据老师ID查询查老师发布的课题
            //List<TopicTitle> topicTitles = new ArrayList<>();
            Example example = new Example(TopicTitle.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("teaId", strings);
            criteria.andEqualTo("status","1");

            List<TopicTitle> topicTitles = topicTitleMapper.selectByExample(example);

            for (TopicTitle t : topicTitles) {
                String substring = stu.getStudentId().substring(0, 4);
                String grade = t.getGrade();
                System.out.println("TeacherTopicService line 108= " + grade+substring);
                if(stu.getStudentId().substring(0,4).equals(t.getGrade())){
                    Map<String, Object> map = new HashMap<>();
                    String count = studentSelectionService.count(t.getTeaId(),grade);
                    Example e = new Example(StudentSelection.class);
                    Example.Criteria c = e.createCriteria();
                    c.andEqualTo("stuId", stu.getId());
                    List<StudentSelection> studentSelections = studentSelectionMapper.selectByExample(e);
                    Boolean flag=false;
                    for (StudentSelection studentSelection : studentSelections) {
                        if(studentSelection.getTopicId().equals(t.getId()))
                            flag=true;
                    }
                    map.put("topicTitles", t);
                    map.put("count", count);
                    map.put("selected", flag);
                    list.add(map);
                }
            }
        }

        if (list != null) {
            return list;
        }
        return null;
    }


    public void stuSelTopic(TopicTitle t){
        //t.getId()
    }


}
